package Inventory;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InventorySelect extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		List<InventoryRec> item = new ArrayList<InventoryRec>();
		
		String InputText= req.getParameter("PID");
	
		if (InputText != null){	
		try {
			Class.forName("org.h2.Driver");
			Connection con = DriverManager.getConnection("jdbc:h2:~/retail","sa", "");
			PreparedStatement ps = con.prepareStatement("SELECT * FROM INVENTORY Where PID=?");
		    ps.setString(1, InputText);
			ResultSet rs = ps.executeQuery();
	
			
						
			while (rs.next()) {
				InventoryRec rec = new InventoryRec();
				rec.setSn(rs.getInt("SN"));
				rec.setPid(rs.getString("PID"));
				rec.setPquan(rs.getInt("PQUANTITY"));
				rec.setEntrydate(rs.getTimestamp("PENTRYDATE"));
				item.add(rec);
			} 
			
			ps.close();
			con.close();
			
			
			
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		} 
	
		req.setAttribute("b", item);
		String action = req.getQueryString();
		if(action.equals("act=select")){
			req.getRequestDispatcher("Inventory/InventorySelect.jsp").forward(req, resp);		
		}		
		if(action.equals("act=out")){
			req.getRequestDispatcher("Inventory/InventoryOut.jsp").forward(req, resp);
		}		
		
	}
		
		
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<InventoryRec> item = new ArrayList<InventoryRec>();
		
		String InputText= req.getParameter("PID");
	
		if (InputText != null){	
		try {
			Class.forName("org.h2.Driver");
			Connection con = DriverManager.getConnection("jdbc:h2:~/retail","sa", "");
			PreparedStatement ps = con.prepareStatement("SELECT * FROM INVENTORY Where PID=? ORDER BY PENTRYDATE DESC");
		    ps.setString(1, InputText);
			ResultSet rs = ps.executeQuery();
	
			
						
			while (rs.next()) {
				InventoryRec rec = new InventoryRec();
				rec.setSn(rs.getInt("SN"));
				rec.setPid(rs.getString("PID"));
				rec.setPquan(rs.getInt("PQUANTITY"));
				rec.setEntrydate(rs.getTimestamp("PENTRYDATE"));
				item.add(rec);
			} 
			
			ps.close();
			con.close();
			
			
			
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		} 
		req.setAttribute("b", item);
		String action = req.getQueryString();
		if(action.equals("act=select")){
			req.getRequestDispatcher("Inventory/InventorySelect.jsp").forward(req, resp);		
		}		
		if(action.equals("act=out")){
			req.getRequestDispatcher("Inventory/InventoryOut.jsp").forward(req, resp);
		}				
	}

}
